package com.example.dubbo.storage.core.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.dubbo.storage.core.utils.HttpUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

@Slf4j
@Component
public class CommandLineRunnerImpl implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        log.info("应用启动完成....");

        new Thread(new Runnable() {
            @Override
            public void run() {
                String body = "{\"productId\":\"17757\",\"productSpecId\":293800,\"activityType\":\"CASH_SCORE\",\"createType\":\"H5NORMAL\",\"specialId\":null,\"scene\":\"\",\"tel\":\"18817520160\",\"userName\":\"麻辣隔壁\",\"address\":\"麻辣隔壁\",\"province\":\"上海市\",\"city\":\"上海市\",\"area\":\"黄埔区\"}";
                Map<String, String> headerMap = Maps.newHashMap();
                headerMap.put("authority", "h5api.lickeji.com");
                headerMap.put("method", "POST");
                headerMap.put("path", "/mall/order/h5CreateOrder");
                headerMap.put("scheme", "https");
                headerMap.put("accept", "application/json, text/javascript, */*; q=0.01");
                headerMap.put("accept-encoding", "gzip");
                headerMap.put("accept-language", "zh-CN,zh;q=0.9");
                headerMap.put("channel", "lickeji");
                headerMap.put("content-length", "249");
                headerMap.put("content-type", "application/json;charset=UTF-8");
                headerMap.put("contenttype", "json");
                headerMap.put("dnt", "1");
                headerMap.put("domainurl", "https");
                headerMap.put("origin", "https");
                headerMap.put("referer", "https");
                headerMap.put("sec-fetch-dest", "empty");
                headerMap.put("sec-fetch-mode", "cors");
                headerMap.put("sec-fetch-site", "cross-site");
                headerMap.put("sign", "f21bd1fc5d61aa66a34f9bb7611022ed");
                headerMap.put("source", "h5");
                headerMap.put("sourceplatform", "huiyuan");
                headerMap.put("timestamp", "1682518999685");
                headerMap.put("token", "be268f87-7149-0fd5-6c6e-b3e0378deb96");
                headerMap.put("user-agent", "Mozilla/5.0");
                headerMap.put("uuid", "be268f87-7149-0fd5-6c6e-b3e0378deb96");

                Map<String, Object> paramMap = Maps.newHashMap();
                JSONObject jsonObject = JSON.parseObject(body);
                for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                    paramMap.put(entry.getKey(), entry.getValue());
                }

                AtomicLong atomicLong = new AtomicLong(0);
                for (int i = 0; i < 2; i++) {

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            for (int j = 0; j < 10000000; j++) {
                                long incrementAndGet = atomicLong.incrementAndGet();
                                try {
                                    String s = HttpUtil.post("https://h5api.lickeji.com/mall/order/h5CreateOrder",
                                            headerMap, paramMap);
                                    log.info("{} {}", incrementAndGet, s);
                                    TimeUnit.MILLISECONDS.sleep(300);
                                } catch (Exception e) {
                                    log.error("exception", e);
                                }
                            }
                        }
                    }).start();
                }
            }
        }).start();
    }
}
